package com.mmlc.meimei.lichengkongjian.mine;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.mmlc.inter.InterActivity;
import com.mmlc.meimei.lichengkongjian.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class AllSellerActivity extends InterActivity {

    @InjectView(R.id.tv_title)
    TextView tvTtitle;
    @InjectView(R.id.iv_back)
    ImageView ivBack;
    @InjectView(R.id.iv_set)
    ImageView ivSet;
    @InjectView(R.id.rg_group)
    RadioGroup rgGroup;
    @InjectView(R.id.vp_viewPager)
    ViewPager vpViewPager;

    @Override
    protected int initContentView() {
        return R.layout.activity_allseller;
    }

    @Override
    protected void initView() {
        ButterKnife.inject(this);

        processData();
    }

    ArrayList<RadioButton> radios = new ArrayList<>();

    private void processData() {
        //处理TitleBar
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ivSet.setVisibility(View.GONE);
        tvTtitle.setText("店铺下单");

        //处理ViewPager
        processViewPager();

        radios.add((RadioButton) findViewById(R.id.rb_0));
        radios.add((RadioButton) findViewById(R.id.rb_1));

        //为RadioGroup添加点击事件
        processRadioGroup();
    }

    int navIndex = 0;

    private void processRadioGroup() {
        rgGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_0://店铺下单
                        navIndex = 0;
                        tvTtitle.setText("店铺下单");
                        break;
                    case R.id.rb_1://推单
                        navIndex = 1;
                        tvTtitle.setText("会员推单");
                        break;
                }
                vpViewPager.setCurrentItem(navIndex, false);
            }
        });
        rgGroup.check(R.id.rb_1);
    }

    private void processViewPager() {
        final List<Fragment> data = new ArrayList<Fragment>();
        data.add(new ShopOrderFragment());
        data.add(new RecommerOrderFragment());
        vpViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {

                return data.get(position);
            }

            @Override
            public int getCount() {
                return data.size();
            }
        });
        vpViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                navIndex = position;
                radios.get(navIndex).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void initEvent() {

    }
}
